<script src="{{ getAssets }}js/amazeui.datatables.min.js?v={{ assets_version }}"></script>
<script language="JavaScript">
var sales_id = '{{.sales_id}}';

var dataTables = $('#dataTables').DataTable({
    order:[]
    ,columns: [
        {"bSortable": false, render:function(data, type, full, meta) {
                return '<input type="checkbox" checked disabled class="database-select database-child" data-collectnum="'+ full.CollectNum +'" data-id="'+ full.Id +'" data-price="'+ full.UnitPrice +'" value="'+ meta.row +'">';
        }}
        ,{"bSortable": false, render:function(data, type, full, meta) {
                return productTypeidName(full.Products.TypeId);
        }}
        ,{"bSortable": false, render:function(data, type, full, meta) {
                return productGroupName(full.Products);
        }}
        ,{"bSortable": false, render:function(data, type, full, meta) {
               var html = [];
               switch (full.Products.TypeId) {
                   case 10:
                       html.push('<span>球镜:'+ full.Snd.sph +'</span></br>');
                       html.push('<span>柱镜:'+ full.Snd.cyl +'</span>');
                       break;
                   case 20:
                       html.push('<span>球镜:'+ full.Snd.sph +'</span></br>');
                       html.push('<span>柱镜:'+ full.Snd.cyl +'</span>');
                       break;
                   case 30:
                       html.push("<span>型号:"+full.Snd.model+"</span></br>");
                       html.push("<span>色号:"+full.Snd.color+"</span></br>");
                       html.push("<span>尺寸:"+full.Snd.size+"</span></br>");
                       html.push("<span>框型:"+full.Snd.genre+"</span>");
                       break;
                   case 40:
                       html.push("<span>球镜:"+full.Snd.sph+"</span></br>");
                       html.push('<span>ADD:'+ full.Snd.add +'</span>');
                       break;
                   case 41:
                       html.push("<span>型号:"+full.Snd.model+"</span></br>");
                       html.push("<span>色号:"+full.Snd.color+"</span></br>");
                       html.push("<span>尺寸:"+full.Snd.size+"</span></br>");
                       html.push("<span>框型:"+full.Snd.genre+"</span></br>");
                       html.push("<span>膜层颜色:"+full.Snd.film_color+"</span></br>");
                       html.push("<span>功效:"+full.Snd.function_info+"</span>");
                       break;
                   case 60:
                       break;
               }
               return html.join('')
        }}
        ,{"bSortable": false, render:function(data, type, full, meta) {
                return full.Inventory.Acceptance.lot_number ? full.Inventory.Acceptance.lot_number : '';
        }}
        ,{"bSortable": false, render:function(data, type, full, meta) {
                return full.Inventory.Acceptance.manufacture_date ? timetransDate(full.Inventory.Acceptance.manufacture_date) : '';
        }}
        ,{"bSortable": false, render:function(data, type, full, meta) {
                return full.Inventory.Acceptance.expiry_date ? timetransDate(full.Inventory.Acceptance.expiry_date) : '';
        }}
        ,{"bSortable": false, render:function(data, type, full, meta) {
                return full.OriginalPrice;
        }}
        ,{"bSortable": false, render:function(data, type, full, meta) {
                return full.DiscountRate;
        }}
        ,{"bSortable": false, render:function(data, type, full, meta) {
                return full.UnitPrice;
        }}
        ,{"bSortable": false, render:function(data, type, full, meta) {
                return full.CollectNum;
        }}
        ,{"width":'8%',"bSortable": false, render:function(data, type, full, meta) {
                return '<input name="return_num" min="0" max="'+ full.CollectNum +'" type="number" value="'+ full.CollectNum +'" onchange="mathReturnPrice();">';
        }}
    ]
    ,"serverSide": false
    ,"bPaginate" : false
    ,"bFilter" : false	//是否启动过滤、搜索功能
    ,"bInfo" : false //是否显示页脚信息，DataTables插件左下角显示记录数
    ,"bProcessing" : true //DataTables载入数据时，是否显示‘进度’提示
    ,"drawCallback": function( settings, json ) {
        mathReturnPrice();
    }
});

//获取单据明细
$.post('{{ urlfor "RetailController.QueryRetailInfo" }}', {'sales_id':sales_id}, function (res) {
    if(res.code == 1) {
        layer.msg(res.msg);return false;
    }
    var retail = res.data.retail;
    var retail_data = res.data.retail_data;
    setHtmlText(retail.Archives); //设置基本资料
    setHtmlText(retail); //设置单据信息

    var tableBox = $('#dataTables').find('tbody');

    var dt = $('#dataTables').DataTable();
    $.each(retail_data, function (index, value) {
        if(value.ReturnNum < value.CollectNum) {
            dt.row.add(value).draw();
        }
    });
}, 'json');

var setHtmlText = function (data) {
    $.each(data, function (idx, val) {
        if(idx == 'HopePickUpTime') {
            val ? $('#'+idx).text(timetransDate(val)) : '';
        } else {
            $('#'+idx).text(val);
        }
    });
};

function mathReturnPrice(){
    var databaseChild = $(".database-child[type='checkbox']:checked");
    var total_return_price = 0.00;
    $.each(databaseChild, function (idx, val) {
        var single_price = $(this).data('price');
        var return_num = $(this).parent().parent().find('input[name="return_num"]').val();
        total_return_price = total_return_price + return_num * single_price;
    });
    var data = $('#payform').serializeObject();
    $("#payinfo").find(".widget-title").html("费用信息--------应退还金额:<span style='color:red;'>￥"+total_return_price+"</span>");
    var return_price = parseFloat(data.AlipayMoney)+parseFloat(data.WechatMoney)+parseFloat(data.CashMoney);
    $("[id='return_price']").val(return_price);
    $('#need_return_price').val((total_return_price-return_price).toFixed(2));
}

function save(){
    var flag = 1;
    var data= $('#payform').serializeObject();
    data.goods_list = [];
    var datatable = $(".database-child[type='checkbox']:checked");
    var total_return_price = 0.00;
    datatable.each(function(i) {
        var id = $(this).data('id');
        var single_price = $(this).data('price');
        var returnnum = $(this).parent().parent().find('input[name="return_num"]').val();
        var collectnum = $(this).data('collectnum');
        data.goods_list.push({'id':Number(id), 'returnnum':Number(returnnum), 'collectnum' : Number(collectnum), "single_price": Number(single_price)});
        total_return_price = total_return_price + returnnum * single_price;
    });
    data.sales_id = sales_id;
    if (datatable.length == 0) {
        layer.alert("请选择需要退货的商品");
        return;
    }
    if(data.return_price<total_return_price){
        layer.alert("请结清所有款项");
        return;
    }else if(data.return_price>total_return_price){
        layer.alert("退款金额不能够大于应退金额");
        return;
    }
    $(data.goods_sale_list).each(function(i) {
        if(this.return_num==0){
            flag=0;
        }else if(this.collectnum<this.return_num){
            flag=2;
        }
    });
    if(flag==0){
        layer.alert("退货数量不能为0");
        return;
    }else if(flag==2){
        layer.alert("退货数量不能大于可退数量");
        return;
    }
    data.goods_list = JSON.stringify(data.goods_list);
    layer.confirm("确认退货信息无误？", function(index) {
        layer.close(index);
        $.post('{{ urlfor "RetailController.SaveConfirmRefund" }}', data, function (res) {
            if(res.code == 1) {
                layer.alert(res.msg);return false;
            } else {
                location = '{{ urlfor "RetailController.RefundQuery" }}';
            }
        }, 'json');
    });
}
</script>